package com.portal.framework.webutil;

import java.io.File;
import java.io.PrintWriter;
import java.io.RandomAccessFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.StrutsResultSupport;

import com.opensymphony.xwork2.ActionInvocation;

public class DownStreamResult extends StrutsResultSupport {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String contentType = "text/html";
	
	private String filePath = null;
	
	private String filename = "unknown";
	
	private String symbol = "FILE";
	
	public DownStreamResult() {
		super();
	}
	
	public String getSymbol() {
		return symbol;
	}

	public void setSymbol(String symbol) {
		this.symbol = symbol;
	}

	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}
	
	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public String getFilename() {
		return filename;
	}

	public void setFilename(String filename) {
		this.filename = filename;
	}

	@Override
	protected void doExecute(String finalLocation, ActionInvocation invocation)
			throws Exception {
		//long sTimer = System.currentTimeMillis();
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		
		File file = (File) invocation.getStack().findValue(conditionalParse(filePath, invocation));
	
		String range = request.getHeader("RANGE");
	
		int startSize = 0;
		int endSize =(int) file.length();
		
		RandomAccessFile rand = new RandomAccessFile(file, "rw");
		int contentLength=endSize - startSize;
		
		if(range != null && !range.equals("")) {
			String[] s;
			if(range.indexOf("=")>=0){
				s = range.split("=")[1].split("-");
			}else{
				s = range.split("-");
			}
			
		    if(s!=null && s.length==1){ 
		    	startSize = (Integer.parseInt(s[0])>0)?Integer.parseInt(s[0]):0;
		    	
		    }else if ((s!=null && s.length==2)){
		    	startSize = (Integer.parseInt(s[0])>0)?Integer.parseInt(s[0]):0;
		    	endSize = (int)((Integer.parseInt(s[1])<file.length())?Integer.parseInt(s[1]):file.length());
		    }
		    if(startSize>=file.length() || startSize>=endSize){
		    	
				PrintWriter out = response.getWriter();
				out.println("error:21");
				out.flush();
				out.close();
	    		return ;
	    	}
		    contentLength = endSize - startSize;
		}
		
		rand.seek(startSize);
		byte[] buffer = new byte[contentLength];
		rand.read(buffer);
		response.setContentType(contentType);
		response.setContentLength(contentLength);
		response.addHeader("Content-disposition", "attachment;filename=" + filename);
		response.setCharacterEncoding("GBK");
		if(symbol.equals("FILE")){
			ServletOutputStream out = response.getOutputStream();
			try {
				out.write(buffer);
				out.flush();
				out.close();		
			} catch (Exception e) {
			   e.printStackTrace();
			}
		} else if(symbol.equals("TXT")) {
			PrintWriter write = response.getWriter();
			write.print(new String(buffer));
			write.flush();
			write.close();
		}		
	
		rand.close();
	}

}
